/// chosen plugin
(function (Handsontable) {
    "use strict";

    var TextAreaEditor = Handsontable.editors.TextEditor.prototype.extend();

    TextAreaEditor.prototype.prepare = function (row, col, prop, td, originalValue, cellProperties) {

        Handsontable.editors.TextEditor.prototype.prepare.apply(this, arguments);

        this.TEXTAREA_VALUE="";
        // this.options = {};
        //
        // if (this.cellProperties.chosenOptions) {
        //     this.options = $.extend(this.options, cellProperties.chosenOptions);
        // }
        // cellProperties.chosenOptions = $.extend({}, cellProperties.chosenOptions);
    };

    TextAreaEditor.prototype.createElements = function () {
        this.$body = $(document.body);

        this.TEXTAREA = document.createElement('textarea');
        //this.TEXTAREA.setAttribute('type', 'text');
        this.$textarea = $(this.TEXTAREA);

        Handsontable.dom.addClass(this.TEXTAREA, 'mytextarea');

        this.textareaStyle = this.TEXTAREA.style;
        this.textareaStyle.width = 0;
        this.textareaStyle.height = 0;

        this.TEXTAREA_PARENT = document.createElement('DIV');
        Handsontable.dom.addClass(this.TEXTAREA_PARENT, 'mytextareadiv');

        this.textareaParentStyle = this.TEXTAREA_PARENT.style;
        this.textareaParentStyle.top = 0;
        this.textareaParentStyle.left = 0;
        this.$textarea.css({
            'min-height': $(this.TD).height() + 200,
            'min-width': $(this.TD).outerWidth() +100
        });
        // this.textareaParentStyle.display = 'none';
        // this.textareaParentStyle.width = "200px";

        this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);

        this.instance.rootElement.appendChild(this.TEXTAREA_PARENT);

        var that = this;
        this.instance._registerTimeout(setTimeout(function () {
            that.refreshDimensions();
        }, 0));
    };

    var onChosenChanged = function () {
        this.close();
        this.finishEditing();
    };
    var onChosenClosed = function () {
        var options = this.cellProperties.chosenOptions;

        if (!options.multiple) {
            this.close();
            this.finishEditing();
        } else {
        }
    };

    TextAreaEditor.prototype.open = function (keyboardEvent) {
        this.refreshDimensions();
        this.textareaParentStyle.display = 'block';
        //
        // this.$textarea.css({
        //     height: $(this.TD).height() + 4,
        //     'min-width': $(this.TD).outerWidth() - 4
        // });
        this.$textarea.css({
            'min-height': $(this.TD).height() + 200,
            'min-width': $(this.TD).outerWidth() +100
        });

        //display the list
        this.$textarea.show();

        //make sure that list positions matches cell position
        //this.$textarea.offset($(this.TD).offset());
        if ($(this.TEXTAREA_PARENT).find(".chosen-container").length) {
            // this.$textarea.chosen("destroy");
        }

        //this.$textarea.chosen(options);

        var self = this;
        setTimeout(function () {

            self.$textarea.trigger("chosen:open");

            $(self.TEXTAREA_PARENT).find("input").on("keydown", function(e) {
                if(e.keyCode === Handsontable.helper.KEY_CODES.ENTER /*|| e.keyCode === Handsontable.helper.KEY_CODES.BACKSPACE*/) {
                    if($(this).val()) {
                        e.preventDefault();
                        e.stopPropagation();
                    } else {
                        e.preventDefault();
                        e.stopPropagation();

                        self.close();
                        self.finishEditing();
                    }

                }

            });

            setTimeout(function () {
                self.$textarea.trigger("chosen:activate").focus();

                if (keyboardEvent && keyboardEvent.keyCode && keyboardEvent.keyCode != 113) {
                    var key = keyboardEvent.keyCode;
                    var keyText = (String.fromCharCode((96 <= key && key <= 105) ? key - 48 : key)).toLowerCase();

                    $(self.TEXTAREA_PARENT).find("input").val(keyText).trigger("keyup.chosen");
                    self.$textarea.trigger("chosen:activate");
                }
            }, 1);
        }, 1);

    };

    TextAreaEditor.prototype.init = function () {
        Handsontable.editors.TextEditor.prototype.init.apply(this, arguments);
    };

    TextAreaEditor.prototype.close = function () {
        this.instance.listen();
        // this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
        this.$textarea.off();
        this.$textarea.hide();
        Handsontable.editors.TextEditor.prototype.close.apply(this, arguments);
    };

    TextAreaEditor.prototype.getValue = function() {
        if(!this.$textarea.val()) {
            return "";
        }
        if(typeof this.$textarea.val() === "object") {
            return this.$textarea.val().join(",");
        }
        return this.$textarea.val();
    };


    TextAreaEditor.prototype.focus = function () {
        this.instance.listen();

        // DO NOT CALL THE BASE TEXTEDITOR FOCUS METHOD HERE, IT CAN MAKE THIS EDITOR BEHAVE POORLY AND HAS NO PURPOSE WITHIN THE CONTEXT OF THIS EDITOR
        //Handsontable.editors.TextEditor.prototype.focus.apply(this, arguments);
    };

    TextAreaEditor.prototype.beginEditing = function (initialValue) {
        var onBeginEditing = this.instance.getSettings().onBeginEditing;
        if (onBeginEditing && onBeginEditing() === false) {
            return;
        }

        Handsontable.editors.TextEditor.prototype.beginEditing.apply(this, arguments);

    };

    TextAreaEditor.prototype.finishEditing = function (isCancelled, ctrlDown) {
        this.instance.listen();
        return Handsontable.editors.TextEditor.prototype.finishEditing.apply(this, arguments);
    };
    Handsontable.editors.mytest = TextAreaEditor;
    Handsontable.editors.registerEditor('mytest', TextAreaEditor);

})(Handsontable);
